import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:vape_xe/utils/utils.dart';
import 'package:vape_xe/widgets/widgets.dart';

class HomeController extends GetxController {
  /// 成员变量
  // 底部导航项目
  late final List<BottomNavigationBarItem> bottomTabs;
  DateTime? _lastPressedAt;

  Future<void> toName(String name) async {
    var data = await Get.toNamed(name);
    await Future.delayed(const Duration(milliseconds: 500));
    if (data != null) {
      String code = Uri.parse(data).queryParameters['c'] ?? '';
      if (code.isEmpty) return;
      Loading.show();
      Request().post('/userVape/scanQrcode', {
        'code': code,
      }).then((result) {
        Get.dialog(ScanDialogView(params: result['result']), barrierDismissible: false);
      });
    }
  }

  void onPopInvoked() {
    final now = DateTime.now();
    if (_lastPressedAt == null || now.difference(_lastPressedAt!) > const Duration(seconds: 1)) {
      _lastPressedAt = now;
      toast('Click again to exit!'.tr, const Duration(milliseconds: 500));
      return;
    }
    SystemNavigator.pop();
  }

  /// 生命周期
  @override
  void onInit() {
    super.onInit();
    // 准备一些静态数据
    bottomTabs = <BottomNavigationBarItem>[
      BottomNavigationBarItem(
        icon: Image.asset(
          "assets/images/tab_ic_home_nor.webp",
          width: 28.w,
          height: 28.w,
        ),
        activeIcon: Image.asset(
          "assets/images/tab_ic_home_pre.webp",
          width: 28.w,
          height: 28.w,
        ),
        label: 'earn',
      ),
      BottomNavigationBarItem(
        icon: Image.asset(
          "assets/images/tab_ic_scan.webp",
          width: 42.w,
          height: 42.w,
        ),
        activeIcon: Image.asset(
          "assets/images/tab_ic_scan.webp",
          width: 42.w,
          height: 42.w,
        ),
        label: 'scan',
      ),
      BottomNavigationBarItem(
        icon: Image.asset(
          "assets/images/tab_ic_pg_nor.webp",
          width: 28.w,
          height: 28.w,
        ),
        activeIcon: Image.asset(
          "assets/images/tab_ic_pg_pre.webp",
          width: 28.w,
          height: 28.w,
        ),
        label: 'playground',
      ),
    ];
  }
}
